/**

 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名:DeptManagerController.java
 * <p>
 * 类产生时间: 2012-5-29 上午12:53:18
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.jxkh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hui.platform.system.exception.HuiException;
import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.constant.ConstantMap;
import com.oceansoft.core.common.util.CommonUtilCache;
import com.oceansoft.core.common.util.DatetimeUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.controller.util.ApiResult;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.domain.SysDept;
import com.oceansoft.core.domain.SysOperLog;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.domain.Evaluation;
import com.oceansoft.core.jxkh.domain.Khy;
import com.oceansoft.core.jxkh.domain.MyMark;
import com.oceansoft.core.jxkh.domain.Ydxj;
import com.oceansoft.core.jxkh.service.IEvaluationService;
import com.oceansoft.core.jxkh.service.IKhyService;
import com.oceansoft.core.jxkh.service.IWorkLogService;
import com.oceansoft.core.service.ISysDeptService;
import com.oceansoft.core.service.ISysUserService;
import com.oceansoft.core.service.IUUMSRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 部门管理控制器
 *
 * @author acer
 *
 */
@Controller
@RequestMapping("/evaluation")
public class EvaluationController {

	@Resource
	private ISysDeptService sysDeptService;
	@Resource
	private IUUMSRoleService roleService;
	@Resource
	private ISysUserService userService;
	@Resource
	private IEvaluationService evaluationService;
	@Resource
	private IKhyService khyService;
	@Resource
	private IWorkLogService workLogService;

	@RequestMapping(value="echartsData.json")
	public void statistics(HttpServletRequest request,ModelMap modelMap) {
		String deptId = request.getParameter("deptId");
		String name = request.getParameter("name");
		SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		// 读取角色
		Map<String, Object> roles = roleService.findRoleByUser(user.getPoliceNum());
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(StringUtil.isNullOrBlank(deptId)){
			deptId = user.getDeptId();
			if((roles.containsKey("10"))||roles.containsKey("11")||roles.containsKey("2")){//指挥长 统计部门的数据
				deptId= deptId.substring(0,6);
				try {
					list =evaluationService.getStatisticsDept(deptId);
				} catch (HuiException e) {
					e.printStackTrace();
				}
			}
		}else{
			//先获取当前点击的部门
			SysDept dept = sysDeptService.findByParentAndName(deptId,name);
			deptId = dept.getDeptId();
			//查询当前传回的deptid是否还有下级部门
			int count = sysDeptService.finddeptcount(deptId);
			if(count>0){
				deptId= deptId.substring(0,8);
				try {
					list =evaluationService.getStatisticsDept(deptId);
				} catch (HuiException e) {
					e.printStackTrace();
				}
			}else{
				try {
					list =evaluationService.getStatisticsUser(deptId);
				} catch (HuiException e) {
					e.printStackTrace();
				}
			}
		}
		JSONObject jsonObject = new JSONObject();
		JSONArray array1= new JSONArray();
		JSONArray array2= new JSONArray();
		for (Map<String, Object> map : list) {
			array1.add(map.get("DEPTNAME"));
//				int count =Integer.parseInt(map.get("USERCOUNT").toString().replaceAll(" ",""));
			int max=99;
			int min=70;
			Random random = new Random();

			int s = random.nextInt(max)%(max-min+1) + min;
			array2.add(s);
		}
		jsonObject.put("name",array1);
		jsonObject.put("data",array2);
		modelMap.addAttribute("data",jsonObject);
		modelMap.addAttribute("deptId",deptId);
	}


	@RequestMapping(value="wdzp.html")
	public String wdzp(HttpServletRequest request,ModelMap modelMap){
		return "evaluation/khcp_zp_list1";
	}


	/**
	 * 我的月度自评/季度自评列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="myEvaluationList.json")
	public void myEvaluationList(HttpServletRequest request,ModelMap modelMap) {
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		String order = request.getParameter("order");
		SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		System.out.println(user.getDbid());
		HashMap<Object,Object> paramap = new HashMap<>();
		paramap.put("offset",offset);
		paramap.put("limit",limit);
		paramap.put("order",order);
		paramap.put("userId",user.getDbid());
		/*Map<String,Object> roles =  (Map<String,Object>) request.getSession().getAttribute(
				Constant.SESSION_ROLOES);
		String type = "0";
		if(roles.containsKey("2")){
			type="0";
		} else
		if(roles.containsKey("10")){//指挥长 统计部门的数据
			type="1";
		} else
		if(roles.containsKey("11")){//处级干部 统计部门的数据
			type="0";
		}*/
		//HashMap<String,String> map = new HashMap<>();
		//map.put("policeNum",user.getPoliceNum());
		Pagination<MyMark> page= null;
		try {
			page = evaluationService.myEvaluation(paramap);
		} catch (HuiException e) {
			e.printStackTrace();
		}
		//modelMap.addAttribute("type",type);
		modelMap.addAttribute("rows",page.getList());
		modelMap.addAttribute("total",page.getTotalRows());
	}

	@RequestMapping(value="findMonthZpByDbid.html")
	public String findMonthZpByDbid(ModelMap modelMap,HttpServletRequest request) throws HuiException{
		SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		String  dbid = request.getParameter("dbid");
		Ydxj ydxj  = workLogService.findLastYdxj(user.getPoliceNum());
		MyMark mark = evaluationService.findMonthZpByDbid(Integer.parseInt(dbid));
		if(com.oceansoft.core.common.util.StringUtil.isNullOrBlank(mark.getWorkLog())){
			mark.setWorkLog(ydxj.getContent());
		}
		modelMap.addAttribute("mark",mark);
		return "jump/khcp_zp";
	}

	@RequestMapping(value="editMonthZp.json" , method = RequestMethod.POST)
	public void editMonthZp(MyMark myMark,ModelMap modelMap) throws HuiException{
		Message message = new Message("操作成功");
		message.setStatus(Constant.RESULT_SUCC);
		try {
			evaluationService.editMonthZp(myMark);
		}catch (Exception e){
			e.printStackTrace();
			message.setStatus(Constant.RESULT_FAILD);
			message.setBody(e.getMessage());
		}
		modelMap.addAttribute("data",message);
	}

	//现在变成所有接口都在这里了
	//以后又时间再分离，演示版本仅供参考
	/**
	 * 我的月度自评/季度自评新增
	 * @param request
	 * @return
	 */
	@RequestMapping(value="selfEvaluation")
	public void selfEvaluation(HttpServletRequest request, Evaluation evaluation,ModelMap modelMap) {
		Message message = new Message("操作成功");
		message.setStatus(Constant.RESULT_SUCC);
		SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		evaluation.setName(user.getName());
		evaluation.setPoliceNum(user.getPoliceNum());
		evaluation.setDeptid(user.getDeptId());
		String year=evaluation.getMonths().substring(0,4);
		String month=evaluation.getMonths().substring(5,7);
		evaluation.setYears(evaluation.getMonths().substring(0,4));
		evaluation.setMonths(evaluation.getMonths().substring(5,7));
		try {
			evaluationService.selfEvaluation(evaluation);
		} catch (HuiException e) {
			e.printStackTrace();
			message.setStatus(Constant.RESULT_FAILD);
			message.setBody(e.getMessage());
		}
		modelMap.addAttribute("data",message);
	}
	/**
	 * 领导评鉴评价
	 * @return
	 */
	@RequestMapping(value="leaderEvaluation")
	public void leaderEvaluation(Evaluation evaluation,ModelMap modelMap) {
		Message message = new Message("操作成功");
		message.setStatus(Constant.RESULT_SUCC);
		try {
			evaluationService.leaderEvaluation(evaluation);
		} catch (HuiException e) {
			e.printStackTrace();
			message.setStatus(Constant.RESULT_FAILD);
			message.setBody(e.getMessage());
		}
		modelMap.addAttribute("data",message);
	}
	/**
	 * 领导评鉴评价列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="leaderElist/{pageIndex}/{policeNum}/{year}")
	public ApiResult leaderElist(HttpServletRequest request,@PathVariable("pageIndex") String pageIndex,@PathVariable("policeNum") String policeNum,@PathVariable("year") String year) {
		ApiResult apiResult = new ApiResult();
		if(StringUtil.isNull(pageIndex))
		{
			pageIndex = "1";
		}
		SysUser user = userService.findUserByPoliceNum(policeNum);
		// 读取角色
		Map<String, Object> roles = roleService.findRoleByUser(policeNum);

		String deptId = user.getDeptId();
		if((roles.containsKey("3"))){//指挥长 统计部门的数据
			deptId= deptId.substring(0,6)+"000000";
		}
		if((roles.containsKey("2"))){//指挥长 统计部门的数据
			deptId= deptId.substring(0,8)+"0000";
		}
		HashMap<String,String> map = new HashMap<>();
		map.put("deptId",deptId);
		map.put("pageIndex",pageIndex);
		Pagination<Evaluation> page= null;
		try {
//			page = evaluationService.leaderElist(map,user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return apiResult.success(page);
	}

	/**
	 * 领导确认
	 * @param request
	 * @return
	 */
	@RequestMapping(value="leaderEvaluation/{dbid}/{policeNum}")
	public ApiResult leaderConfirm(HttpServletRequest request,@PathVariable("dbid") String dbid,@PathVariable("policeNum") String policeNum) {
		ApiResult apiResult = new ApiResult();
		try {
			evaluationService.leaderConfirm(dbid);
		} catch (HuiException e) {
			e.printStackTrace();
		}
		return apiResult;
	}
	@RequestMapping(value="ldpj_list/{type}.html")
	public String ldpj_list(HttpServletRequest request,ModelMap model,@PathVariable("type") String type) {
		SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		/*Map<String,Object> roles =  (Map<String,Object>) request.getSession().getAttribute(
				Constant.SESSION_ROLOES);
		String deptId = user.getDeptId();
		if(roles.containsKey("2")){
			deptId="";
		} else
		if(roles.containsKey("10")){//指挥长 统计部门的数据
			deptId= deptId.substring(0,6)+"000000";
		} else
		if(roles.containsKey("11")){//处级干部 统计部门的数据
			deptId= deptId.substring(0,8)+"0000";
		}*/
		HashMap<String,String> map = new HashMap<>();
		Map<String,String> dateMap = DatetimeUtil.getLastMonth();
//		map.put("deptId",deptId);
		map.put("type",type);
		map.put("firstDay",dateMap.get("firstDay"));
		map.put("lastDay",dateMap.get("lastDay"));
		List<Evaluation> list= null;
		try {
			list = evaluationService.leaderElist(map,user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("list",list);
		model.addAttribute("type",type);
		return "evaluation/ldpj_list";
	}

	@RequestMapping(value="ldpj/{dbid}.html")
	public String ldpj(HttpServletRequest request,ModelMap model,@PathVariable("dbid") String dbid) {
		SysUser user = (SysUser) request.getSession().getAttribute(Constant.LOGIN_USER);
		String type = request.getParameter("type");
		Evaluation evaluation = evaluationService.findEvaluation(Integer.parseInt(dbid));
		Map<String,String> khymap =  new HashMap<String,String>();
		try {
			List<Khy> khylist = khyService.findAllKhy();
			for(int i=0;i<khylist.size();i++){
				khymap.put("Amax"+i,String.valueOf(khylist.get(i).getAmax()));
				khymap.put("Amin"+i,String.valueOf(khylist.get(i).getAmin()));
				khymap.put("Bmax"+i,String.valueOf(khylist.get(i).getBmax()));
				khymap.put("Bmin"+i,String.valueOf(khylist.get(i).getBmin()));
				khymap.put("Cmax"+i,String.valueOf(khylist.get(i).getCmax()));
				khymap.put("Cmin"+i,String.valueOf(khylist.get(i).getCmin()));
				khymap.put("Dmax"+i,String.valueOf(khylist.get(i).getDmax()));
				khymap.put("Dmin"+i,String.valueOf(khylist.get(i).getDmin()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("data",evaluation);
		model.addAttribute("khymap",khymap);
		model.addAttribute("type",type);
		//model.addAttribute("list",page.getList());
		return "evaluation/ldpj_edit";
	}


	/**
	 * 领导确认
	 * @param request
	 * @return
	 */
	@RequestMapping(value="confirmData/{dbid}")
	public void leaderConfirm(HttpServletRequest request,@PathVariable("dbid") String dbid,ModelMap modelMap) {
		Message message = new Message("操作成功");
		message.setStatus(Constant.RESULT_SUCC);
		try {
			evaluationService.leaderConfirm(dbid);
		} catch (HuiException e) {
			e.printStackTrace();
			message.setStatus(Constant.RESULT_FAILD);
			message.setBody(e.getMessage());
		}
		modelMap.addAttribute("data",message);
	}
	@RequestMapping(value="bmkhpg.html")
	public String index(HttpServletRequest request,ModelMap model) {
		return "deptkh/bmkhpg";
	}

}
